<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="rowList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="attributePermission(row)"
                  >
                    分配权限
                  </el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRoles(row)"
                  >
                    编辑
                  </el-button>
                  <el-button size="small" type="danger" @click="delRoles(row)">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                background
                :total="total"
                :current-page="params.page"
                :page-size="params.pagesize"
                @current-change="changepage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司管理" />
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增角色 -->
    <el-dialog title="新增角色" :visible="showPop" @close="closePop">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showPop = false">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showPermission"
      width="30%"
      @close="closePermission"
    >
      <el-tree
        ref="tree"
        node-key="id"
        :data="permissionList"
        :props="defaultProps"
        show-checkbox
        default-expand-all
        check-strictly
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="showPermission = false">取 消</el-button>
        <el-button type="primary" @click="submitPermission">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, addRole, deleteRole, getRoleDetail, updateRole } from '@/api/employees'
import { getPermissionList } from '@/api/permission'
import { toTreeData } from '@/utils'
import { assignPerm } from '@/api/setting'
export default {
  data () {
    return {
      currentRoleId: '',
      permissionList: [],
      showPermission: false,
      showPop: false,
      params: { page: 1, pagesize: 10 },
      total: 0,
      rowList: [],
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created () {
    this.getEmployees()
  },
  methods: {
    async submitPermission () {
      const select = this.$refs.tree.getCheckedKeys()
      try {
        await assignPerm({ id: this.currentRoleId, permIds: select })
        this.showPermission = false
        this.$message.success('添加权限成功')
      } catch (error) {
        console.log(error)
      }
    },
    async attributePermission (row) {
      this.showPermission = true
      this.currentRoleId = row.id
      this.getperList()
      try {
        const res = await getRoleDetail(row.id)
        this.$refs.tree.setCheckedKeys(res.permIds)
      } catch (error) {
        console.log(error)
      }
    },
    async getperList () {
      try {
        const res = await getPermissionList()
        this.permissionList = toTreeData(res)
      } catch (error) {
        console.log(error)
      }
    },
    closePermission () {
      this.showPermission = false
      this.$refs.tree.setCheckedKeys([])
    },
    changepage (curpage) {
      console.log(curpage)
      this.params.page = curpage
      this.getEmployees()
    },
    async editRoles (data) {
      this.showPop = true
      try {
        const res = await getRoleDetail(data.id)
        this.roleForm = res
      } catch (error) {
        console.log(error)
      }
    },
    async delRoles (data) {
      try {
        await this.$confirm('确定删除吗', '提示')
        await deleteRole(data.id)
        this.$message.success('删除成功')
        this.getEmployees()
      } catch (error) {
        console.log(error)
      }
    },
    async btnOK () {
      try {
        await this.$refs.roleForm.validate()
        if (!this.roleForm.id) {
          await addRole(this.roleForm)
        } else {
          console.log('gengxin')
          await updateRole(this.roleForm)
        }

        this.showPop = false
        this.getEmployees()
        this.roleForm = {
          name: '',
          description: ''
        }
      } catch (error) {
        console.log(error)
      }
    },
    closePop () {
      this.showPop = false
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    async getEmployees () {
      try {
        const res = await getRoleList(this.params)
        this.total = res.total
        this.rowList = res.rows
      } catch (error) {
        console.log(error)
      }
    },
    addRole () {
      this.showPop = true
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
